import Router from "koa-router";

import { PERFIX_URL } from "@/config/router";

import { mysqlQuery } from "@/model/mysql";

const KoaRouter = new Router();
KoaRouter.prefix(`${PERFIX_URL}/mysqluser`);

// 查找
KoaRouter.get("/find", async (ctx) => {
  const { query } = ctx.query;
  const res = await mysqlQuery("SELECT * FROM list", []);
  ctx.status = 200;
  ctx.body = {
    success: true,
    data: res.results,
    message: "mysql查找接口",
  };
});

// 删除
KoaRouter.delete("/delete", async (ctx) => {
  const { age } = ctx.query;
  const res = await mysqlQuery("DELETE FROM list where age = ?", [age]);
  ctx.status = 200;
  ctx.body = {
    success: true,
    data: res.results,
    message: "mysql删除接口",
  };
});

// 编辑
KoaRouter.patch("/edit", async (ctx) => {
  const { editName } = ctx.query;
  const { name, age } = ctx.request.body;
  const res = await mysqlQuery(
    "UPDATE list SET name = ?,age = ? WHERE name = ?",
    [name, age, editName]
  );
  ctx.status = 200;
  ctx.body = {
    success: true,
    data: res.results,
    message: "mysql编辑接口",
  };
});

// 添加
KoaRouter.post("/add", async (ctx) => {
  const { name, age } = ctx.request.body;
  const res = await mysqlQuery(`INSERT INTO list(name,age) VALUES(?,?)`, [
    name,
    age,
  ]);
  ctx.status = 200;
  ctx.body = {
    success: true,
    data: res.results,
    message: "mysql添加接口",
  };
});
export default KoaRouter;
